
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include( "shared.lua" )
include( "sv_foodsystem.lua" )
include( "round_controller.lua" )

function GM:Initialize()

	if ( GAMEMODE.GameLength > 0 ) then
		timer.Simple( GAMEMODE.GameLength * 60, function() GAMEMODE:EndOfGame( true ) end )
		SetGlobalFloat( "GameEndTime", CurTime() + GAMEMODE.GameLength * 60 )
	end
	
	if ( GAMEMODE.RoundBased ) then
		timer.Simple( 2, function() GAMEMODE:StartRoundBasedGame() end )
	end
			
end

function GM:KeyPress ( pl, key )

	if !pl:GetNWBool("IsDying") then
		if key == IN_ATTACK then
			local trace = pl:GetEyeTrace()
			if string.find( trace.Entity:GetClass(), "food_" ) and pl:GetPos():Distance(trace.HitPos) < 108 then
				BeginEating( pl, trace.Entity, trace.Entity.EatTime, trace.Entity:GetDTFloat(0) )
		elseif trace.Entity:IsPlayer() and key == IN_ATTACK2 then
					if CheckCanShove( pl ) and trace.Entity:GetNWBool("IsEating") then
						trace.Entity:SetVelocity(pl:GetForward() * 500 + Vector(0,100,0))
						InterruptEating(trace.Entity, pl, pl:GetNWEntity("CurFood"))
						pl.NextShove = CurTime() + GAMEMODE.ShoveDelay
					end
			end
		end
	else
		pl:SetNWInt("ButtonsLeft", pl:GetNWInt("ButtonsLeft") - 1)
		if pl:GetNWInt("ButtonsLeft") <= 0 then
			pl:SetNWBool("IsDying", false)
			local weight = pl:GetNWInt("weight")
			local newspeed = math.Clamp( 450-(3*weight), 50, 450 )
			GAMEMODE:SetPlayerSpeed( pl, newspeed, newspeed )
			pl:SetJumpPower( 350-(weight*2) )
			pl:UnSpectate()
			pl:SetViewEntity(pl)
			if pl:GetNWEntity( "Cam" ) then pl:GetNWEntity( "Cam" ):Remove() end
		end
	end

end

function CheckCanShove( pl )

	if pl.NextShove <= CurTime() then
		return true
	else
		return false
	end

end

function GM:KeyRelease ( pl, key )

	if key == IN_ATTACK and pl:GetNWBool("IsEating") then
		local trace = pl:GetEyeTrace()
			InterruptEating( pl, nil, trace.Entity )
	end

end

function GM:Think()

end

function SeenSplash( ply )

	if ( ply.m_bSeenSplashScreen ) then return end
	ply.m_bSeenSplashScreen = true

end

concommand.Add( "seensplash", SeenSplash )

function GM:OnPreRoundStart( num )

	game.CleanUpMap()
	
		for k,v in pairs( player.GetAll() ) do
			v:Spawn()
			v:SetAngles( Angle(0, 0, 0) )
			v:Freeze(true)
		end

end

function GM:OnRoundStart( )

	for k,v in pairs( player.GetAll() ) do
	
		v:Freeze(false)
		v:SetNWInt( "weight", 0 )
		
	end
	
end

function GM:PlayerInitialSpawn( pl )

	pl:SetModel("models/gentleman.mdl")
	
	pl:SetNWBool("IsEating", false)
	pl:SetPlayerClass( "Default" )
	pl.m_bFirstSpawn = true
	pl:UpdateNameColor()
	GAMEMODE:SetPlayerSpeed( pl, 250, 250 )
	
	GAMEMODE:CheckPlayerReconnected( pl )
	pl:SetNWInt("Red", 255)
	pl:SetNWInt("Green", 255)
	pl:SetNWInt("Blue", 255)

end

function GM:DoPlayerDeath( pl, attacker, dmginfo )

	pl:CreateRagdoll()

end

function GM:ShouldDrawLocalPlayer()
	return true
end

function UpdatePlayerColours( pl )

	pl:SetNWInt("Red", pl:GetInfoNum("selfRed"))
	pl:SetNWInt("Green", pl:GetInfoNum("selfGreen"))
	pl:SetNWInt("Blue", pl:GetInfoNum("selfBlue"))
	
end
concommand.Add("UpdatePlayerColours", UpdatePlayerColours)

function GM:PlayerSpawn( pl ) 


		-- pl:SetViewEntity( mdlview )
	
	pl:CrosshairDisable( true )

	pl:SetPlayerClass( "Default" )

	pl:StopParticles()
	
	pl:UpdateNameColor()

	if ( pl.m_bFirstSpawn ) then
	
		pl.m_bFirstSpawn = nil
		
		if ( pl:IsBot() ) then
					if ( !GAMEMODE.TeamBased && !GAMEMODE.NoAutomaticSpawning ) then
				pl:Spawn()
			end

		else
		
			pl:StripWeapons()
			
		end
	
		return
		
	end
		
	pl:CheckPlayerClassOnSpawn()
	
	pl:UnSpectate()

	hook.Call( "PlayerLoadout", GAMEMODE, pl )
	
	hook.Call( "PlayerSetModel", GAMEMODE, pl )
	
	pl:OnSpawn()
	
	pl.NextShove = CurTime()
	
end

function GM:CheckRoundEnd()

	if ( !GAMEMODE:InRound() ) then return end

	for k,v in pairs( player.GetAll() ) do

		if v:GetNWInt("weight") >= 100 then

		      GAMEMODE:RoundEndWithResult( v )

                end

        end

end

function GM:OnRoundWinner( ply, resulttext )

	if GetGlobalInt( "RoundNumber" ) < self.RoundLimit then
		ply:Kill()
		--ParticleEffectAttach("goop_blast", PATTACH_ABSORIGIN_FOLLOW, ply, 0)
	end
	
end

function GM:SelectCurrentlyWinningPlayer()

	local winner
	local topweight = 0

	for k,v in pairs( player.GetAll() ) do

		if v:GetNWInt("weight") > topweight then
			winner = v
			topweight = v:GetNWInt("weight")

		end

	end

	return winner

end

function GM:RoundTimerEnd()

	if ( !GAMEMODE:InRound() ) then return end
	
		local ply = GAMEMODE:SelectCurrentlyWinningPlayer()
		if ply then
			GAMEMODE:RoundEndWithResult( ply, "Time Up, " .. ply:Nick() .. " is this round's Top Chomper!" )
		else
			GAMEMODE:RoundEndWithResult( -1, "Time Up" )
		end

end